import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/views/home.vue'
import dataShow from '@/components/DataShow.vue'
import Mine from '@/components/Mine.vue'
import AppUser from '@/components/AppUser.vue'
import ShopUser from '@/components/ShopUser.vue'
import ManageUser from '@/components/ManageUser.vue'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/datashow',
      children: [
        {
          path: '/datashow',
          component: dataShow
        }, {
          path: '/mine',
          component: Mine
        }, {
          path: '/appUser',
          component: AppUser
        }, {
          path: '/shopUser',
          component: ShopUser
        }, {
          path: '/manageUser',
          component: ManageUser
        }
      ]
    }
  ]
})
// 如果用户没有登录，但是直接通过URL访问特定页面，需要重新导航到登录页面。
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
